<template>
  <div class="history-container">
    <!-- 标题和搜索框区域 -->
    <div class="header">
      <h2 class="history-title">历史记录</h2>
      <div class="search-box">
        <input 
          type="text" 
          v-model="searchKeyword" 
          placeholder="请输入你要查找的内容"  
          class="search-input"
          @input="handleSearch"
        >
        <text class="search-icon">🔍</text>
      </div>
    </div>
    
    <!-- 空状态提示 -->
    <div v-if="filteredHistory.length === 0" class="empty-tip">
      暂无历史记录
    </div>
    
    <!-- 历史记录列表 -->
    <ul v-else class="history-list">
      <li 
        v-for="item in filteredHistory" 
        :key="item.id" 
        class="history-item"
        @click="viewHistoryDetail(item)"  
      >
        <div class="history-content-container">
          <div class="history-content">{{ item.content }}</div>
          <div class="history-actions">
            <button 
              class="star-btn" 
              @click.stop="toggleStar(item)"  
              :class="{ starred: isStarred(item.id) }"
            >
              {{ isStarred(item.id) ? '★' : '☆' }}
            </button>
            <button 
              class="delete-btn" 
              @click.stop="deleteHistory(item.id)"
            >
              🗑️
            </button>
          </div>
        </div>
        <div class="history-time">{{ item.time }}</div>
      </li>
    </ul>
    
    <!-- 历史详情弹窗 -->
    <div class="detail-mask" v-if="showDetail" @click="closeDetail"></div>
    <div class="detail-container" v-if="showDetail">
      <div class="detail-header">
        <h3 class="detail-title">对话详情</h3>
        <button class="close-btn" @click="closeDetail">×</button>
      </div>
      <div class="detail-content">
        <div class="detail-item user-item">
          <div class="role-label">问题：</div>
          <div class="message-content">{{ currentHistory.question }}</div>
        </div>
        <div class="detail-item ai-item">
          <div class="role-label">回答：</div>
          <div class="message-content">{{ currentHistory.answer }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      historyList: [],
      filteredHistory: [],
      starList: [],
      searchKeyword: '',
      showDetail: false,
      currentHistory: null
    }
  },
  onShow() {
    this.loadHistory();
    this.loadStars();
  },
  methods: {
    loadHistory() {
      const list = uni.getStorageSync('historyList') || [];
      this.historyList = Array.isArray(list) ? list.sort((a, b) => b.id - a.id) : [];
      this.filteredHistory = [...this.historyList];
    },
    
    // 加载收藏列表
    loadStars() {
      const stars = uni.getStorageSync('starList') || [];
      this.starList = Array.isArray(stars) ? stars : [];
    },
    
    // 检查是否已收藏
    isStarred(id) {
      return this.starList.some(item => item.id === id);
    },
    
    // 切换收藏状态
    toggleStar(item) {
      const isAlreadyStarred = this.isStarred(item.id);
      
      if (isAlreadyStarred) {
        // 取消收藏
        this.starList = this.starList.filter(star => star.id !== item.id);
        uni.showToast({ title: '已取消收藏', icon: 'none' });
      } else {
        // 添加收藏
        this.starList.push({...item});
        uni.showToast({ title: '收藏成功', icon: 'success' });
      }
      
      // 保存到本地存储
      uni.setStorageSync('starList', this.starList);
    },
    
    // 处理搜索逻辑
    handleSearch() {
      const keyword = this.searchKeyword.trim().toLowerCase();
      
      if (!keyword) {
        this.filteredHistory = [...this.historyList];
        return;
      }
      
      this.filteredHistory = this.historyList.filter(item => {
        const content = (item.content || '').toLowerCase();
        const question = (item.question || '').toLowerCase();
        const answer = (item.answer || '').toLowerCase();
        
        return content.includes(keyword) || 
               question.includes(keyword) || 
               answer.includes(keyword);
      });
    },
    
    // 查看历史详情
    viewHistoryDetail(item) {
      this.currentHistory = item;
      this.showDetail = true;
    },
    
    // 关闭详情弹窗
    closeDetail() {
      this.showDetail = false;
      this.currentHistory = null;
    },
    
    // 删除历史记录
    deleteHistory(id) {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除这条历史记录吗？',
        confirmText: '删除',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            // 从历史列表中移除
            this.historyList = this.historyList.filter(item => item.id !== id);
            this.filteredHistory = this.filteredHistory.filter(item => item.id !== id);
            
            // 同时从收藏列表中移除（如果存在）
            this.starList = this.starList.filter(item => item.id !== id);
            
            // 保存到本地存储
            uni.setStorageSync('historyList', this.historyList);
            uni.setStorageSync('starList', this.starList);
            
            // 显示删除成功提示
            uni.showToast({
              title: '删除成功',
              icon: 'success'
            });
          }
        }
      });
    }
  }
}
</script>

<style scoped>
.history-container {
  padding: 24rpx 20rpx;
  background: linear-gradient(135deg, #eaf4fe 0%, #b3d9ff 100%);
  min-height: 100vh;
}

/* 头部区域 - 标题和搜索框 */
.header {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  margin-bottom: 24rpx;
}

.history-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin: 0;
}

/* 搜索框样式 */
.search-box {
  position: relative;
  width: 100%;
}

.search-input {
  width: 100%;
  padding: 14px 14px; 
  font-size: 15px;
  border: 1px solid #ddd; 
  border-radius: 12px;
  background-color: #fff;
  box-sizing: border-box;
  outline: none;
  min-height: 50px; 
  padding-left: 70rpx; 
}

.search-input::placeholder {
  color: #666; 
  font-size: 15px; 
  letter-spacing: normal;
}

.search-icon {
  position: absolute;
  left: 2rpx;
  top: 50%;
  transform: translateY(-50%);
  font-size: 26rpx;
  color: #999;
}

/* 空状态提示 */
.empty-tip {
  color: #aaa;
  font-size: 28rpx;
  text-align: center;
  margin-top: 80rpx;
}

/* 历史记录列表 */
.history-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.history-item {
  /* background: linear-gradient(135deg, #abebe5 0%, #b3d9ff 100%); */
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(177, 223, 223, 0.942);
  margin-bottom: 18rpx;
  padding: 22rpx 18rpx;
  display: flex;
  flex-direction: column;
  cursor: pointer; /* 显示点击指针 */
  background-color: #f0f7ff;
}



.history-content-container {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.history-content {
  font-size: 30rpx;
  color: #222;
  margin-bottom: 8rpx;
  word-break: break-all;
  flex: 1;
  padding-right: 10rpx;
}

/* 操作按钮容器 */
.history-actions {
  display: flex;
  gap: 10rpx;
}

/* 收藏按钮样式 */
.star-btn {
  background: none;
  border: 1px solid #ddd;
  width: 50rpx;
  height: 50rpx;
  border-radius: 6rpx;
  font-size: 36rpx;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ccc;
  transition: all 0.2s;
  padding: 0;
  flex-shrink: 0; /* 防止按钮被压缩 */
}

.star-btn.starred {
  color: #ffd700;
  border-color: #ffd700; 
  background-color: rgba(255, 215, 0, 0.1);
}

/* 删除按钮样式 */
.delete-btn {
  background: none;
  border: 1px solid #ff4d4f;
  width: 50rpx;
  height: 50rpx;
  border-radius: 6rpx;
  font-size: 28rpx;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ff4d4f;
  transition: all 0.2s;
  padding: 0;
  flex-shrink: 0;
}

.delete-btn:hover {
  background-color: rgba(255, 77, 79, 0.1);
}

.history-time {
  font-size: 24rpx;
  color: #999;
  text-align: right;
}

/* 详情弹窗样式 */
.detail-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.detail-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90%;
  max-width: 600rpx;
  background-color: #fff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
  z-index: 1000;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.detail-header {
  padding: 24rpx;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 36rpx;
  color: #999;
  cursor: pointer;
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.detail-content {
  padding: 24rpx;
  overflow-y: auto;
  flex: 1;
}

.detail-item {
  margin-bottom: 30rpx;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.role-label {
  font-size: 28rpx;
  font-weight: 600;
  margin-bottom: 12rpx;
  color: #666;
}

.user-item .role-label {
  color: #1890ff;
}

.ai-item .role-label {
  color: #722ed1;
}

.message-content {
  font-size: 28rpx;
  line-height: 1.6;
  color: #333;
  padding: 20rpx;
  border-radius: 8rpx;
}

.user-item .message-content {
  background-color: #e6f7ff;
}

.ai-item .message-content {
  background-color: #f9f0ff;
}

/* 适配不同屏幕尺寸 */
@media (min-width: 768px) {
  .search-input {
    font-size: 16px;
    padding: 15px 16px;
  }
  
  .search-input::placeholder {
    font-size: 16px;
  }
  
  .search-icon {
    font-size: 28rpx;
    left: 20rpx;
  }
}

@media (max-width: 375px) {
  .search-input {
    padding: 12px 12px;
    min-height: 48px;
    font-size: 14px;
  }
}
</style>